libotutil: new function ot_openat_ignore_enoent
authorGiuseppe Scrivano <gscrivan@redhat.com>
Fri, 11 Mar 2016 10:36:21 +0000 (11:36 +0100)
committerGiuseppe Scrivano <gscrivan@redhat.com>
Tue, 15 Mar 2016 08:48:47 +0000 (09:48 +0100)
Refactor some common code

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
src/libostree/ostree-repo-refs.c
src/libostree/ostree-repo.c
src/libotutil/ot-fs-utils.c
src/libotutil/ot-fs-utils.h

index bf480067fec8f102c125c90a72703f9e789e6e04..0c03ad1d17c9e483d8c0f18feee52de936e37181 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "ostree-repo-private.h"
 #include "otutil.h"
+#include "ot-fs-utils.h"
 
 static gboolean
 add_ref_to_set (const char       *remote,
@@ -114,31 +115,6 @@ write_checksum_file_at (OstreeRepo   *self,
   return ret;
 }
 
-static gboolean
-openat_ignore_enoent (int dfd,
-                      const char *path,
-                      int *out_fd,
-                      GError **error)
-{
-  gboolean ret = FALSE;
-  int target_fd = -1;
-  
-  target_fd = openat (dfd, path, O_CLOEXEC | O_RDONLY);
-  if (target_fd < 0)
-    {
-      if (errno != ENOENT)
-        {
-          glnx_set_error_from_errno (error);
-          goto out;
-        }
-    }
-
-  ret = TRUE;
-  *out_fd = target_fd;
- out:
-  return ret;
-}
-
 static gboolean
 find_ref_in_remotes (OstreeRepo         *self,
                      const char         *rev,
@@ -168,7 +144,7 @@ find_ref_in_remotes (OstreeRepo         *self,
       if (!glnx_opendirat (dfd_iter.fd, dent->d_name, TRUE, &remote_dfd, error))
         goto out;
 
-      if (!openat_ignore_enoent (remote_dfd, rev, &ret_fd, error))
+      if (!ot_openat_ignore_enoent (remote_dfd, rev, &ret_fd, error))
         goto out;
 
       if (ret_fd != -1)
@@ -247,21 +223,21 @@ resolve_refspec (OstreeRepo     *self,
     {
       const char *remote_ref = glnx_strjoina ("refs/remotes/", remote, "/", ref);
 
-      if (!openat_ignore_enoent (self->repo_dir_fd, remote_ref, &target_fd, error))
+      if (!ot_openat_ignore_enoent (self->repo_dir_fd, remote_ref, &target_fd, error))
         goto out;
     }
   else
     {
       const char *local_ref = glnx_strjoina ("refs/heads/", ref);
 
-      if (!openat_ignore_enoent (self->repo_dir_fd, local_ref, &target_fd, error))
+      if (!ot_openat_ignore_enoent (self->repo_dir_fd, local_ref, &target_fd, error))
         goto out;
 
       if (target_fd == -1)
         {
           local_ref = glnx_strjoina ("refs/remotes/", ref);
 
-          if (!openat_ignore_enoent (self->repo_dir_fd, local_ref, &target_fd, error))
+          if (!ot_openat_ignore_enoent (self->repo_dir_fd, local_ref, &target_fd, error))
             goto out;
 
           if (target_fd == -1)
index 752e097a55374b476db090ded12cec3d6266afc0..2398c46f1e0574fb000577ec21fdc4eb713f3009 100644 (file)
@@ -34,6 +34,7 @@
 #include "ostree-repo-file-enumerator.h"
 #include "ostree-gpg-verifier.h"
 #include "ostree-repo-static-delta-private.h"
+#include "ot-fs-utils.h"
 
 #ifdef HAVE_LIBSOUP
 #include "ostree-metalink.h"
@@ -2591,32 +2592,6 @@ list_loose_objects (OstreeRepo                     *self,
   return ret;
 }
 
-static gboolean
-openat_allow_noent (int                 dfd,
-                    const char         *path,
-                    int                *fd,
-                    GCancellable       *cancellable,
-                    GError            **error)
-{
-  GError *temp_error = NULL;
-
-  if (!gs_file_openat_noatime (dfd, path, fd,
-                               cancellable, &temp_error))
-    {
-      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
-        {
-          *fd = -1;
-          g_clear_error (&temp_error);
-        }
-      else
-        {
-          g_propagate_error (error, temp_error);
-          return FALSE;
-        }
-    }
-  return TRUE;
-}
-
 static gboolean
 load_metadata_internal (OstreeRepo       *self,
                         OstreeObjectType  objtype,
@@ -2638,14 +2613,14 @@ load_metadata_internal (OstreeRepo       *self,
 
   _ostree_loose_path (loose_path_buf, sha256, objtype, self->mode);
 
 if (!openat_allow_noent (self->objects_dir_fd, loose_path_buf, &fd,
-                           cancellable, error))
if (!ot_openat_ignore_enoent (self->objects_dir_fd, loose_path_buf, &fd,
+                               error))
     goto out;
 
   if (fd < 0 && self->commit_stagedir_fd != -1)
     {
-      if (!openat_allow_noent (self->commit_stagedir_fd, loose_path_buf, &fd,
-                               cancellable, error))
+      if (!ot_openat_ignore_enoent (self->commit_stagedir_fd, loose_path_buf, &fd,
+                                    error))
         goto out;
     }
 
@@ -2839,8 +2814,8 @@ ostree_repo_load_file (OstreeRepo         *self,
       struct stat stbuf;
       g_autoptr(GInputStream) tmp_stream = NULL;
 
-      if (!openat_allow_noent (self->objects_dir_fd, loose_path_buf, &fd,
-                               cancellable, error))
+      if (!ot_openat_ignore_enoent (self->objects_dir_fd, loose_path_buf, &fd,
+                                    error))
         goto out;
 
       if (fd != -1)
index b709befebeda18e02a5597300a5dc32438ed2fe0..ec27d762bc486f3febc2bc80ce88cafb358175c7 100644 (file)
@@ -205,3 +205,28 @@ ot_ensure_unlinked_at (int dfd,
     }
   return TRUE;
 }
+
+gboolean
+ot_openat_ignore_enoent (int dfd,
+                         const char *path,
+                         int *out_fd,
+                         GError **error)
+{
+  gboolean ret = FALSE;
+  int target_fd = -1;
+
+  target_fd = openat (dfd, path, O_CLOEXEC | O_RDONLY);
+  if (target_fd < 0)
+    {
+      if (errno != ENOENT)
+        {
+          glnx_set_error_from_errno (error);
+          goto out;
+        }
+    }
+
+  ret = TRUE;
+  *out_fd = target_fd;
+ out:
+  return ret;
+}
index 10686be68e1c22f92d5896b485ab6a07fd7717df..cfeea74d42d830d85e861d7c0f78d1282cec8203 100644 (file)
@@ -61,4 +61,9 @@ gboolean ot_ensure_unlinked_at (int dfd,
                                 const char *path,
                                 GError **error);
 
+gboolean ot_openat_ignore_enoent (int dfd,
+                                  const char *path,
+                                  int *out_fd,
+                                  GError **error);
+
 G_END_DECLS